package com.xhu.rjsx2.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;
import com.xhu.rjsx2.enums.AppHttpCodeEnum;
import com.xhu.rjsx2.pojo.Member;
import com.xhu.rjsx2.pojo.PayProject;
import com.xhu.rjsx2.pojo.ResponseResult;
import com.xhu.rjsx2.pojo.dto.MemberDTO;
import com.xhu.rjsx2.pojo.dto.PayProjectDTO;
import com.xhu.rjsx2.pojo.vo.LayuiPageResult;
import com.xhu.rjsx2.pojo.vo.MemberVO;
import com.xhu.rjsx2.pojo.vo.PageVO;
import com.xhu.rjsx2.service.PayProjectService;
import com.xhu.rjsx2.util.BeanCopyUtils;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;

@RequestMapping("/payProject")
@RestController
@Api(tags = "付费项目（pay_project）管理接口API",description = "PayProject Controller")
public class PayProjectController {

    @Resource
    private PayProjectService payProjectService;
    @GetMapping("/listAllPayProject")
    @ApiOperation(value = "查询所有数据接口")
    public ResponseResult listAllPayProject(){

        List<PayProject> payProjectList = payProjectService.list();
        return ResponseResult.okResult(payProjectList);
    }

    @GetMapping("/listAllPayProjectPage")
    @ApiOperation(value = "列表分页查询接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "当前页数"),
            @ApiImplicitParam(name = "limit", value = "每页条数"),
            @ApiImplicitParam(name = "projectName", value = "项目名称"),
            @ApiImplicitParam(name = "projectPriceMin", value = "价格区间-最小值"),
            @ApiImplicitParam(name = "projectPriceMax", value = "价格区间-最小值"),
    })
    public LayuiPageResult listAllPayProjectPage(Integer page, Integer limit, String projectName, BigDecimal projectPriceMin,BigDecimal projectPriceMax){

        PageVO pageVO = payProjectService.listAllPayProjectPage(page,limit,projectName,projectPriceMin,projectPriceMax);
        return new LayuiPageResult(LayuiPageResult.CODE_SUCCESS,LayuiPageResult.MESSAGE_SUCCESS,pageVO.getRows(), pageVO.getTotal());
    }


    @PostMapping("/updatePayProjectById")
    @ApiOperation(value = "通过主键更新项目信息")
    public ResponseResult updatePayProjectById(@ApiParam(value = "项目传输对象",required = true) @RequestBody PayProjectDTO payProjectDTO){
        PayProject payProject = BeanCopyUtils.copyBeanSingle(payProjectDTO, PayProject.class);
        String now = DateUtil.now();
        payProject.setProjectDate(DateUtil.parse(now));
        boolean updateSuccess = payProjectService.updateById(payProject);
        return updateSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
    @PostMapping("/deletePayProjectById")
    @ApiOperation(value = "通过主键删除项目信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "当前项目id")
    })
    public ResponseResult deletePayProjectById(Integer id){
        boolean removeSuccess = payProjectService.removeById(id);
        return removeSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }
    @PostMapping("/insertPayProject")
    @ApiOperation(value = "新增项目接口")
    public ResponseResult insertPayProject(@ApiParam(value = "项目传输对象,不需要输入id字段(输了也没用)")@RequestBody PayProjectDTO payProjectDTO){
        // 禁止用户传入id，由系统生成
        payProjectDTO.setId(null);
        PayProject payProject = BeanCopyUtils.copyBeanSingle(payProjectDTO, PayProject.class);
        String now = DateUtil.now();
        payProject.setProjectDate(DateUtil.parse(now));
        boolean saveSuccess = payProjectService.save(payProject);
        return saveSuccess ? ResponseResult.okResult() : ResponseResult.errorResult(AppHttpCodeEnum.SYSTEM_ERROR);
    }

}
